package com.hq.system.service.impl;


import com.hq.system.mapper.UserMapper;
import com.hq.system.model.User;
import com.hq.system.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description
 * @Author zlf
 * @Date 2021/9/8 9:58
 * @Version 1.0
 **/

public class UserServiceImpl implements UserService {
    @Override
    public User queryUserByUser(User user) {
        User user_db = null;
        InputStream in = null;
        //1.读取配置文件
        try {
            in = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
            //2.船舰SqlSessionFactory
            //先创建创建者builder
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //再创建factory工厂传入in
            SqlSessionFactory factory = builder.build(in);
            //3.使用工厂生产S起来session对象
            SqlSession sqlSession = factory.openSession();
            //4.使用Sqlsession创建Mapper对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            //业务
            user_db =userMapper.queryUserByUser(user);
            //提交事务
            sqlSession.commit();
            //释放资源
            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return user_db;
    }

    @Override
    public List<User> queryAllUser() {
        List<User> user_dbList=null;
        InputStream in = null;
        //1.读取配置文件
        try {
            in = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
            //2.船舰SqlSessionFactory
            //先创建创建者builder
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //再创建factory工厂传入in
            SqlSessionFactory factory = builder.build(in);
            //3.使用工厂生产S起来session对象
            SqlSession sqlSession = factory.openSession();
            //4.使用Sqlsession创建Mapper对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            //业务
            user_dbList =userMapper.queryAllUser();
            //提交事务
            sqlSession.commit();
            //释放资源
            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return user_dbList;
    }

    @Override
    public int deleteUserByuser_id(int user_id) {
        int n = 0;
        InputStream in = null;
        //1.读取配置文件
        try {
            in = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
            //2.船舰SqlSessionFactory
            //先创建创建者builder
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //再创建factory工厂传入in
            SqlSessionFactory factory = builder.build(in);
            //3.使用工厂生产S起来session对象
            SqlSession sqlSession = factory.openSession();
            //4.使用Sqlsession创建Mapper对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            //业务
            n =userMapper.deleteUserByuser_id(user_id);
            //提交事务
            sqlSession.commit();
            //释放资源
            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return n;
    }

    @Override
    public int addUserByUser(User user) {
        int n = 0;
        InputStream in = null;
        //1.读取配置文件
        try {
            in = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
            //2.船舰SqlSessionFactory
            //先创建创建者builder
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //再创建factory工厂传入in
            SqlSessionFactory factory = builder.build(in);
            //3.使用工厂生产S起来session对象
            SqlSession sqlSession = factory.openSession();
            //4.使用Sqlsession创建Mapper对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            //业务
            n =userMapper.addUserByUser(user);
            //提交事务
            sqlSession.commit();
            //释放资源
            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return n;
    }

    @Override
    public User queryUserByuser_id(int user_id) {
        User user_db=null;
        InputStream in = null;
        //1.读取配置文件
        try {
            in = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
            //2.船舰SqlSessionFactory
            //先创建创建者builder
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //再创建factory工厂传入in
            SqlSessionFactory factory = builder.build(in);
            //3.使用工厂生产S起来session对象
            SqlSession sqlSession = factory.openSession();
            //4.使用Sqlsession创建Mapper对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            //业务
            user_db =userMapper.queryUserByuser_id(user_id);
            //提交事务
            sqlSession.commit();
            //释放资源
            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return user_db;
    }

    @Override
    public int updateUserByUser(User user) {
        int n = 0;
        InputStream in = null;
        //1.读取配置文件
        try {
            in = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
            //2.船舰SqlSessionFactory
            //先创建创建者builder
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //再创建factory工厂传入in
            SqlSessionFactory factory = builder.build(in);
            //3.使用工厂生产S起来session对象
            SqlSession sqlSession = factory.openSession();
            //4.使用Sqlsession创建Mapper对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            //业务
            n =userMapper.updateUserByUser(user);
            //提交事务
            sqlSession.commit();
            //释放资源
            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return n;
    }
    //注入Mapper代理接口



}
